package com.gxa.xiaoucomeshome.controller;

import com.gxa.xiaoucomeshome.service.CourierService;
import com.gxa.xiaoucomeshome.utils.FormatTool;
import com.gxa.xiaoucomeshome.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 万骞
 * @date 2022-06-07
 */

@Api(value = "courierController", tags = "配送模块相关的api接口")
@RestController
@RequestMapping("/courier")
public class CourierController {
    @Autowired
    CourierService courierService;

    @ApiOperation(value = "配送员登录", produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "配送员登录密码", paramType = "query")
    })
    @PostMapping("/login")
    public Map login(String phone, String password, @ApiIgnore HttpSession session) throws Exception {
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        session.setAttribute("token", token);

        courierService.login(phone, password);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功！");
        jsonResult.set("token", token);

        return jsonResult.getMap();
    }

    @ApiOperation("个人中心当前登录者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
    })
    @PostMapping("/selectByPhone")
    public Map selectByPhone(String phone, @RequestHeader(value = "token", required = false) String token) {
        Map<String, Object> map = courierService.selectByPhone(phone);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(map);

        return jsonResult.getMap();
    }

    @ApiOperation("配送员首页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "siteName", value = "站点名字", paramType = "query"),
    })
    @PostMapping("/selectBySiteName")
    public Map selectBySiteName(String siteName, @RequestHeader(value = "token", required = false) String token) throws Exception {
        List<Map<String, Object>> maps = courierService.selectBySiteName(siteName);

        FormatTool formatTool = new FormatTool();
        for (Map<String, Object> map : maps) {
           formatTool.dateFormat(map,"arrived_time");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(maps);
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心抢单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "订单编号", paramType = "query"),
    })
    @PostMapping("/updateForGrabOrders")
    public Map updateForGrabOrders(String phone, String id, @RequestHeader(value = "token", required = false) String token) throws Exception {
        courierService.updateForGrabOrders(phone, id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("抢单成功");
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心待取货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
    })
    @PostMapping("/pendingPickup")
    public Map selectPendingPickup(String phone, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Map<String, Object> pendingPickup = courierService.selectPendingPickup(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(pendingPickup);
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心待送货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
    })
    @PostMapping("/pendingDelivery")
    public Map selectPendingDelivery(String phone, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Map<String, Object> pendingDelivery = courierService.selectPendingDelivery(phone);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(pendingDelivery);
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心签收成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
    })
    @PostMapping("/signedSuccessfully")
    public Map selectSignedSuccessfully(String phone, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Map<String, Object> signedSuccessfully = courierService.selectSignedSuccessfully(phone);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(signedSuccessfully);
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心签收失败")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "配送员手机号", paramType = "query"),
    })
    @PostMapping("/failedToSign")
    public Map selectFailedToSign(String phone, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Map<String, Object> failedToSign = courierService.selectFailedToSign(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(failedToSign);
        return jsonResult.getMap();
    }

    @ApiOperation("配送员个人中心修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", paramType = "query"),
            @ApiImplicitParam(name = "newPassword2", value = "重复输入新密码", paramType = "query")
    })
    @PostMapping("/updatePassword")
    public Map updatePassword(String phone, String oldPassword, String newPassword, String newPassword2,
                              @RequestHeader(value = "token", required = false) String token) throws Exception {
        courierService.updatePassword(phone, oldPassword, newPassword, newPassword2);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("密码修改成功");
        return jsonResult.getMap();
    }

    @ApiOperation("订单列表——>待取货")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "phone", value = "配送员电话", dataType = "string", paramType = "query")
    })
    @PostMapping("/getListPendingPickUp")
    public Map pendingPickUp(@RequestParam String phone, @RequestHeader(value = "token", required = true) String token) throws Exception {
        List<Map<String, Object>> listPendingPickUp = courierService.getListPendingPickUp(phone);

        FormatTool formatTool = new FormatTool();
        for (Map<String, Object> map : listPendingPickUp) {
            formatTool.dateFormat(map, "dispatch_time");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(listPendingPickUp);
        return jsonResult.getMap();
    }

    @ApiOperation("确认取货")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")
    })
    @PostMapping("/pickup")
    public Map pickUp(@RequestParam String orderid, @RequestHeader(value = "token", required = true) String token) throws Exception {
        boolean b = courierService.pickUp(orderid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg(String.valueOf(b));
        return jsonResult.getMap();
    }

    @ApiOperation("订单列表——>待送货")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "phone", value = "配送员电话", dataType = "string", paramType = "query")

    })
    @PostMapping("/getListPendingDelivery")
    public Map pendingDelivery(@RequestParam String phone,
                               @RequestHeader(value = "token", required = true) String token) throws Exception {
        List<Map<String, Object>> listPendingDelivery = courierService.getListPendingDelivery(phone);

        FormatTool formatTool = new FormatTool();
        for (Map<String, Object> map : listPendingDelivery) {
            formatTool.dateFormat(map, "pickup_time");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(listPendingDelivery);
        return jsonResult.getMap();
    }

    @ApiOperation("确认签收")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "signIn", value = "是否签收成功", dataType = "boolean"),
            @ApiImplicitParam(name = "desc", value = "签收情况(成功/失败情况)", dataType = "string"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "string"),
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "url1", value = "图片1", dataType = "string"),
            @ApiImplicitParam(name = "url1", value = "图片2", dataType = "string")
    })
    @PostMapping("/signIn")
    public Map signIn(@RequestParam boolean signIn, @RequestParam String orderid, @RequestParam String desc,
                      @RequestParam String remark, @RequestParam(required = false) String url1,
                      @RequestParam(required = false) String url2,
                      @RequestHeader(value = "token", required = true) String token) throws Exception {
        boolean b = courierService.signIn(signIn, desc, remark, url1, url2, orderid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg(String.valueOf(b));
        return jsonResult.getMap();
    }

    @ApiOperation("订单列表——>签收成功")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "phone", value = "配送员电话", dataType = "string", paramType = "query")
    })
    @PostMapping("/signInSuccess")
    public Map signInSuccess(@RequestParam String phone, @RequestHeader(value = "token", required = true) String token) throws Exception {
        List<Map<String, Object>> listSignInSuccess = courierService.getListSignInSuccess(phone);

        FormatTool formatTool = new FormatTool();
        for (Map<String, Object> map : listSignInSuccess) {
            formatTool.dateFormat(map, "signin_time");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(listSignInSuccess);
        return jsonResult.getMap();
    }

    @ApiOperation("订单列表——>签收失败")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "phone", value = "配送员电话", dataType = "string", paramType = "query")
    })
    @PostMapping("/signInFailed")
    public Map signInFailed(@RequestParam String phone, @RequestHeader(value = "token", required = true) String token) throws Exception {
        List<Map<String, Object>> listSignInFailed = courierService.getListSignInFailed(phone);

        FormatTool formatTool = new FormatTool();
        for (Map<String, Object> map : listSignInFailed) {
            formatTool.dateFormat(map, "signin_time");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(listSignInFailed);
        return jsonResult.getMap();
    }

    @ApiOperation("订单详情——>待取货")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")})
    @PostMapping("/pendingPickUpDetail")
    public Map pendingPickUpDetail(@RequestParam String orderid, @RequestHeader(value = "token", required = true) String token) throws Exception {
        Map<String, Object> stringObjectMap = courierService.pendingPickUpDetail(orderid);

        FormatTool formatTool = new FormatTool();
        formatTool.dateFormat(stringObjectMap, "dispatch_time");


        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(stringObjectMap);
        return jsonResult.getMap();
    }

    @ApiOperation("订单详情——>待送货")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")
    })
    @PostMapping("/pendingDeliveryDetail")
    public Map pendingDeliveryDetail(@RequestParam String orderid,
                                     @RequestHeader(value = "token", required = true) String token) throws Exception {
        Map<String, Object> stringObjectMap = courierService.pendingDeliveryDetail(orderid);

        FormatTool formatTool = new FormatTool();
        formatTool.dateFormat(stringObjectMap, "pickup_time");


        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(stringObjectMap);
        return jsonResult.getMap();
    }

    @ApiOperation("订单详情——>签收成功")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")

    })
    @PostMapping("/signInSuccessDetail")
    public Map signInSuccessDetail(@RequestParam String orderid, @RequestHeader(value = "token", required = true) String token) throws Exception {
        Map<String, Object> stringObjectMap = courierService.signInSuccessDetail(orderid);

        FormatTool formatTool = new FormatTool();
        formatTool.dateFormat(stringObjectMap, "signin_time");


        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(stringObjectMap);
        return jsonResult.getMap();
    }

    @ApiOperation("订单详情——>签收失败")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")
    })
    @PostMapping("/signInFailedDetail")
    public Map signInFailedDetail(@RequestParam String orderid,
                                  @RequestHeader(value = "token", required = true) String token) throws Exception {
        Map<String, Object> stringObjectMap = courierService.signInFailedDetail(orderid);

        FormatTool formatTool = new FormatTool();
        formatTool.dateFormat(stringObjectMap, "signin_time");

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(stringObjectMap);
        return jsonResult.getMap();
    }

    @ApiOperation("退回功能")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "reason", value = "退回原因", dataType = "string"),
            @ApiImplicitParam(name = "orderid", value = "订单编号", dataType = "string", paramType = "query")
    })
    @PostMapping("/sendBack")
    public Map sendBack(@RequestParam String reason, @RequestParam String orderid, @RequestHeader(value = "token", required = true) String token) throws Exception {
        boolean b = courierService.sendBack(reason, orderid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg(String.valueOf(b));
        return jsonResult.getMap();
    }

    @ApiOperation("退出登录 ")
    @PostMapping("/logout")
    public void logout(@ApiIgnore HttpSession session) {
        session.invalidate();
    }
}
